/* eslint valid-jsdoc: "off" */

"use strict";

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {
    env: "prod",
    rundir: "/tmp",
    logger: {
      dir: "/tmp",
    },
  });
  //配置后台地址
  config.adminPath = "admin";

  //配置文件上传的模式
  config.multipart = {
    mode: "file",
    fields: 20,
  };
  config.uploadDir = "app/public/upload";

  //配置排除的地址
  config.ignoreUrl = [
    `/${config.adminPath}`,
    `/${config.adminPath}/welcome`,
    `/${config.adminPath}/login/loginOut`,
  ];

  //Cos地址 建议配置成自己的域名
  config.cosUrl = "https://serverless-1301377264.cos.ap-guangzhou.myqcloud.com";

  config.session = {
    key: "SESSION_ID_itying",
    maxAge: 30 * 60 * 1000, //如果过期时间比较长 建议保存在redis里面
    httpOnly: true,
    encrypt: true,
    renew: true, // 延长会话有效期
  };

  //配置cos
  config.cosObject = {
    SecretId: "AKIDynOkrlQmvrZ2HH3btwkJcgV5QWNE0kT3",
    SecretKey: "PbaJn22neJjvue9RUJsCNM7ewznNiphK",
    Bucket: "serverless-1301377264",
    Region: "ap-guangzhou",
  };

  //配置csrf https://eggjs.org/zh-cn/core/security.html
  config.security = {
    csrf: {
      // 判断是否需要 ignore 的方法，请求上下文 context 作为第一个参数
      ignore: (ctx) => {
        if (ctx.request.url == `/${config.adminPath}/product/doUpload`) {
          return true;
        } else if (ctx.request.url.indexOf("/api") != -1) {
          return true;
        } else {
          return false;
        }
      },
    },
    domainWhiteList: ["http://localhost:8081"],
  };

  config.cors = { origin: "*", allowMethods: "GET,PUT,POST,DELETE" };

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1576384476895_3620";

  // add your middleware config here
  config.middleware = ["adminAuth"];
  config.adminAuth = {
    match: `/${config.adminPath}`, //注意
    adminPath: config.adminPath,
    cosUrl: config.cosUrl,
  };

  // add your user config here
  const userConfig = {
    view: {
      mapping: {
        ".html": "ejs",
      },
    },
  };

  //配置数据库
  /*   config.sequelize = {
    dialect: "mysql",
    host: "gz-cdb-hz37n411.sql.tencentcdb.com",
    port: 58329,
    username: "root",
    password: "yuqian20001116",
    database: "eggshop",
  }; */

  config.sequelize = {
    dialect: "mysql",
    host: "127.0.0.1",
    port: 3306,
    username: "root",
    password: "123456",
    database: "eggshop",
  };

  //配置jwt
  config.jwt = {
    secret: "123456xxx",
  };

  config.io = {
    init: {}, // passed to engine.io
    namespace: {
      "/": {
        connectionMiddleware: ["connection"], //建立连接的时候触发方法
        packetMiddleware: [],
      },
    },
  };

  return {
    ...config,
    ...userConfig,
  };
};
